package com.dq.service.sys.user.impl;

import com.dq.context.Constant;
import com.dq.domain.sys.user.user.WxUser;
import com.dq.exception.customException.AdminException;
import com.dq.domain.sys.menu.SysMenu;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.repository.BaseRepositories;
import com.dq.service.ShiroService;
import com.dq.service.sys.user.SysUserService;
import com.dq.utils.PasswordUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @创建人 gewenzhen
 * @创建时间 2019/8/15
 * @描述
 */
@Service
public class SysUserServiceImpl extends BaseRepositories implements SysUserService {

    @Override
    public List<SysMenu> queryAllPerms(SysUser sysUser) {
        return dao.find("select distinct(rm.sysMenu) from SysUserRole ur , SysRoleMenu rm  where ur.sysRole=rm" +
                ".sysRole and ur.sysUser=?0 and rm.sysMenu.type=?1", new Object[]{sysUser, SysMenu.Type.button});
    }

    @Override
    public List<SysMenu> findAllByUser(SysUser user) {
        return dao.find("select distinct(rm.sysMenu) from SysUserRole ur , SysRoleMenu rm  where ur.sysRole=rm" +
                ".sysRole and ur.sysUser=?0 and ur.sysRole.deleted = false and rm.sysMenu.deleted=false ", new
                Object[]{user});
    }

    @Override
    public Long countAllByUser(SysUser sysUser) {
        return dao.count("select count(rm.sysMenu) from SysUserRole ur , SysRoleMenu rm  where ur.sysRole=rm" +
                ".sysRole and ur.sysUser=?0 and ur.sysRole.deleted = false and rm.sysMenu.deleted=false ", new Object[]{sysUser});
    }

    /**
     * 获取用户信息
     */
    @Override
    public SysUser getUser() throws AdminException {
        SysUser loginUser = shiroService.getUser();
        if (null == loginUser) {
            throw new AdminException("您还未登陆");
        }

        Long userId = loginUser.getId();
//        Long userId = 1L;
        loginUser = dao.get(SysUser.class, userId);
        if (null == loginUser || loginUser.getId() == null) {
            throw new AdminException("登录信息在数据库不存在");
        }

        return loginUser;
    }
    /**
     * 获取登录用户信息和门店信息
     */
    @Override
    public SysUser getUserInSection() throws AdminException {
        SysUser loginUser = getUser();
        if(!(loginUser instanceof WxUser)) {
            if(loginUser.getSection() == null || loginUser.getSection().getId() == null) {
                throw new AdminException("用户组织部门信息异常");
            }
        }

        return loginUser;
    }


    /**
     * 返回加密密码
     */
    @Override
    public String getPassword(String password) {
        if(StringUtils.isNotBlank(password)) {
            if(password.length() < Constant.PASSWORD_SIZE) {
                throw new AdminException("密码 位数少于" + Constant.PASSWORD_SIZE);
            }
            return PasswordUtil.generate(password);
        }else {
            throw new AdminException("密码 不能为空");
        }
    }

}
